/**
 * Created by 邵亚楠 on 2018/9/8.
 */
var sessionId = sessionStorage.getItem('sessionId');
//var sessionId = 'oQ_An0pr2htuLDV42lwE1LeRM220'
$(function(){
    var id = getQueryString("bookId")
    //console.log(id)
    //var sessionId = 'oQ_An0pr2htuLDV42lwE1LeRM220'
    //获取书籍详情
    $.ajax({
        url:'https://www.bjdeshangyuan.com/book/api/detail/bookDetail',
        type:'POST',
        data:{'sessionid':sessionId,bookId:id},
        success:function(r){
            //console.log(r.data)
            var newData = r.data
            if(r.code ==='USR000'){
                $('.pic_book').css('background','url(http://www.bjdeshangyuan.com/book'+newData.img+')0% 0% / 100% 100% no-repeat')
                var bookTitle = newData.name
                $('.book-name').text(newData.name)
                $('.user').text(newData.author)
                $('.count').text(newData.buyCount)
                $('#author').text(newData.authorIntroduce)
                $('.author').text(newData.bookIntroduce)
            }
        }
    })

    //书友评论列表
    $.ajax({
        url:'https://www.bjdeshangyuan.com/book/api/comment/commentList',
        type:'POST',
        data:{'sessionid':sessionId,'bookId':id},
        success:function(r){
            if(r.code ==='USR000'){
                var newDataList = r.data.commentList
                $('.count').text(r.data.commentCounts)
                for(var i=0;i<newDataList.length;i++){
                    //$('.like_box .like').removeClass('bgc')
                    var newList = newDataList[i]
                    //console.log(newList)
                    var id = newList.id
                    var isLike = newDataList[i].isLike
                    console.log(isLike)
                    var icon_class_name = isLike == 0 ? '' : 'bgc';
                    var html =
                        '<div class="comment_item">'+
                                '<div class="pic">'+
                                    '<img src="'+newList.photo+'" alt="">'+
                                '</div>'+
                                '<div class="comment-detail">'+
                                    '<p class="user_box">' +
                                        '<span>'+newList.userName+'</span>' +
                                        '<span class="like_box" onclick="getLike('+id+')" data-id = '+id+'>' +
                                            '<span class="like '+icon_class_name+'"></span><span class="count">'+newList.goods+'</span>'+
                                        '</span></p>'+
                                    '<p class="text">'+newList.bookcontent+'</p>'+
                                '</div>'+
                        '</div>'

                    $('.comment_list').prepend(html)

                }
            }
        }
    })


    $('.top a').click(function(){
        $(this).addClass('active').siblings().removeClass('active')
        var index = $(this).index()
        $('.detail').eq(index).show().siblings().hide()
    })

    $('.sum').click(function(){
        window.location.href = 'PictureCommentCoun.html'
    })

    $('.liuyan').click(function(){
        window.location.href = 'comment.html?bookId='+id+''+'&type=bookDetail'+''
    })
    $('.back').click(function(){
        window.history.back()
    })

})


function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//评论点赞
function getLike(id){
    $.ajax({
        url:'https://www.bjdeshangyuan.com/book/api/comment/apiAddGood',
        type:'POST',
        data:{'sessionid':sessionId,'commentId':id},
        success:function(r){
            if(r.code ==='USR000'){
                //layer.msg('点赞成功',{
                //    time:1000
                //})
                //console.log(r)
                var Count = r.data.thumbUpTimes
                var isLike = r.data.isLike
                //console.log(isLike)
                $('.like_box').each(function(){
                    var ref = $(this)
                    if(ref.attr('data-id') == id){
                        if(isLike == 0){
                            ref.find('.like').removeClass('bgc')
                        }else{
                            ref.find('.like').addClass('bgc')
                        }
                        ref.find('.count').text(Count)
                    }
                })
            }else if(r.code === 'USR103'){
                //layer.msg('您已点赞',{
                //    time:1000
                //})
            }
        }
    })
}